/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove301.jianghongtiao.liaad.prisoner.ga.crossover;

import java.util.Random;
import sk.lieskove301.jianghongtiao.liaad.prisoner.agent.Agent;
import sk.lieskove301.jianghongtiao.liaad.prisoner.gene.GeneEnum;
import sk.lieskove301.jianghongtiao.liaad.prisoner.gene.GeneticInfo;
import sk.lieskove301.jianghongtiao.liaad.prisoner.memory.ForgettingEnum;

/**
 * Crossover is made on single place of genetic code. As genetic code is considered
 * 5 genes of behavior, Type of Memory, Normal/Inverted and Positive/Negative values. </br>
 * In this order we treat genes:
 * <ol>
 *   <li>What to do in first step (when I'm without memories)</li>
 *   <li>What to do when both of us were cooperating</li>
 *   <li>What to do when I cooperated and opponent betrayed</li>
 *   <li>What to do when I betrayed and opponent cooperated</li>
 *   <li>What to do when both of us betrayed</li>
 *   <li>Type of agent's memory</li>
 *   <li>If agent is normal or inverted</li>
 *   <li>If agent is positive or negative</li>
 * </ol>
 * 
 * Single Crossover generate random number based on normal distribution. This number 
 * is value from interval 1..7, so at least one gene will be changed. 
 * 
 * Date of create: May 26, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0526
 */
public class SinglePointCrossover implements Crossover{
    
    private Random rand = new Random();

    public Agent cross(Agent firstAgent, Agent secondAgent) {
        int randomVal = rand.nextInt(7);
        Agent result = firstAgent.copyAgent();
        GeneEnum[] gene = result.getGene().getGene();
        if(randomVal <= 0){
            gene[0] = secondAgent.getGene().get(0);
        }
        if(randomVal <= 1){
            gene[1] = secondAgent.getGene().get(1);
        }
        if(randomVal <= 2){
            gene[2] = secondAgent.getGene().get(2);
        }
        if(randomVal <= 3){
            gene[3] = secondAgent.getGene().get(3);
        }
        if(randomVal <= 4){
            gene[4] = secondAgent.getGene().get(4);
        }
        if(randomVal <= 5){
            result.setForgetting(secondAgent.getForgetting());
        }
        if(randomVal <= 6){
            result.setInvert(secondAgent.getInvert());
        }
        if(randomVal <= 7){
            result.setOptimistic(secondAgent.getOptimistic());
        }
        result.setGene(new GeneticInfo(gene));
        return result;
    }
    
}
